package com.hopes.bizz.risk.hid.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.hid.dto.RiskHidHiddenDangerExcelDTO;
import com.hopes.bizz.risk.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.risk.hid.listener.ImageModifyHandler;
import com.hopes.bizz.risk.hid.service.RiskHidHiddenDangerFileService;
import com.hopes.bizz.risk.hid.service.RiskHidHiddenDangerService;
import com.hopes.bizz.risk.hid.vo.RiskHidHiddenDangerRecheckExcelVO;
import com.hopes.boss.admin.api.feign.RemoteFileService;
import com.hopes.boss.common.core.constant.SecurityConstants;
import com.hopes.boss.common.data.tenant.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * @date 2023/8/4 10:26
 * @Description: 导出隐患复查记录
 */
@Service("riskHidHiddenDangerRecheckFile")
@Slf4j
public class RiskHidHiddenDangerRecheckFileServiceImpl implements RiskHidHiddenDangerFileService {

    @Autowired
    private RiskHidHiddenDangerService hidHiddenDangerService;

    @Autowired
    private RemoteFileService remoteFileService;

    @Override
    public void exportList(RiskHidHiddenDangerExcelDTO riskHidHiddenDangerExcelDTO, ServletOutputStream out) {
        List<RiskHidHiddenDangerEntity> list = hidHiddenDangerService.lambdaQuery()
                .eq(StringUtils.isNotBlank(riskHidHiddenDangerExcelDTO.getHidSource()),RiskHidHiddenDangerEntity::getHidSource,riskHidHiddenDangerExcelDTO.getHidSource())
                .eq(StringUtils.isNotBlank(riskHidHiddenDangerExcelDTO.getHidLevel()),RiskHidHiddenDangerEntity::getHidLevel,riskHidHiddenDangerExcelDTO.getHidLevel())
                .eq(StringUtils.isNotBlank(riskHidHiddenDangerExcelDTO.getHidType()),RiskHidHiddenDangerEntity::getHidType,riskHidHiddenDangerExcelDTO.getHidType())
                .eq(StringUtils.isNotBlank(riskHidHiddenDangerExcelDTO.getDeptId()),RiskHidHiddenDangerEntity::getDeptId,riskHidHiddenDangerExcelDTO.getDeptId())
                .ge(ObjectUtil.isNotEmpty(riskHidHiddenDangerExcelDTO.getReportStartTime()),RiskHidHiddenDangerEntity::getReportDate,riskHidHiddenDangerExcelDTO.getReportStartTime())
                .le(ObjectUtil.isNotEmpty(riskHidHiddenDangerExcelDTO.getReportEndTime()),RiskHidHiddenDangerEntity::getReportDate,riskHidHiddenDangerExcelDTO.getReportEndTime())
                .list();
        List<RiskHidHiddenDangerRecheckExcelVO> exportList = new ArrayList<>();
        HashMap<String, List<byte[]>> images = new HashMap<>();
        HashMap<Integer, Integer> columnImageMostCount = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            RiskHidHiddenDangerRecheckExcelVO vo = new RiskHidHiddenDangerRecheckExcelVO();
            BeanUtils.copyProperties(list.get(i), vo);
            exportList.add(vo);
            String signature = list.get(i).getCheckUserSignature();
            buildImagesByte(images,columnImageMostCount, i, 10, signature);

            String reportPhoto = list.get(i).getReportPhoto();
            buildImagesByte( images,columnImageMostCount, i, 7, reportPhoto);

            String correctiveReportPhoto = list.get(i).getCorrectiveReportPhoto();
            buildImagesByte(images,columnImageMostCount, i, 8, correctiveReportPhoto);
        }
        // 写入数据
        EasyExcel.write(out, RiskHidHiddenDangerRecheckExcelVO.class).registerWriteHandler(new ImageModifyHandler(images,columnImageMostCount)).sheet().doWrite(exportList);
    }

    private void buildImagesByte( HashMap<String, List<byte[]>> images, HashMap<Integer, Integer> columnImageMostCount, int row, int column, String photoIdStr) {
        if (StringUtils.isNotEmpty(photoIdStr)) {
            String[] photoIds = photoIdStr.split(",");
            columnImageMostCount.compute(column,(k,v)->{
                if (v == null || v< photoIds.length){
                    return photoIds.length;
                }
                return v;
            });
            ArrayList<byte[]> bytes = new ArrayList<>();
            for (String photoId : photoIds) {
                String imgName = photoId.split(StringUtils.SLASH)[photoId.split(StringUtils.SLASH).length - 1];
                String tenantBucketName = TenantContextHolder.getTenantBucketName();
                byte[] imgByte = remoteFileService.getFileStream(tenantBucketName, imgName, SecurityConstants.FROM_IN);
                bytes.add(imgByte);
                images.put(row + 1 + "," + column, bytes);
            }
        }
    }
}
